import React,{useState,useEffect} from 'react'
import ReactECharts from 'echarts-for-react'
import './cusState.css'
import Service from '../../../../service'

const Index =()=> {
  const [allNum,setAllNum] = useState([{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0}])
  const [todayNum,setTodayNum] = useState([{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0}])
  const [weekNum,setWeekNum] = useState([{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0}])
  const [monthNum,setMonthNum] = useState([{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0},{count1:0}])

  useEffect(()=>{
    getCustomerStateAll() 
    getCustomerStateToday()
    getCustomerStateWeek()
    getCustomerStateMonth()
  },[])
  
  const getCustomerStateAll = async ()=>{
    let resultAll = await Service.dataAnalysis.customerStateAll()
    setAllNum(resultAll.data);
    let temarr = []
    resultAll.data.map(item=>{
      if(!isNaN(item.count1)){
        temarr.push(item.count1)
      };
    })
    setMaxNum(temarr.sort((a,b)=>{return b-a})[0]);
  }
  const getCustomerStateToday = async ()=>{
    let resultToday = await Service.dataAnalysis.customerStateToday()
    setTodayNum(resultToday.data);
  }
  const getCustomerStateWeek = async ()=>{
    let resultWeek = await Service.dataAnalysis.customerStateWeek()
    setWeekNum(resultWeek.data);
  }
  const getCustomerStateMonth = async ()=>{
    let resultMonth = await Service.dataAnalysis.customerStateMonth()
    setMonthNum(resultMonth.data);
  }
  const [maxNum,setMaxNum] = useState(0)  // 设置最大值

  let option = {
    legend: {},
    tooltip: {trigger: 'axis',},
    toolbox: {
      feature: {
        saveAsImage: { show: true }
      }
    },
    dataset: {
      dimensions: ['sysState','一天内', '一周内', '一月内', '总计'],
      source: [
        { sysState: '客户管理','一天内': todayNum[0].count1,'一周内': weekNum[0].count1, '一月内': monthNum[0].count1, '总计': allNum[0].count1},
        { sysState: '联系人', '一天内': todayNum[1].count1,'一周内': weekNum[1].count1, '一月内': monthNum[1].count1, '总计': allNum[1].count1},
        { sysState: '跟单管理 ','一天内': todayNum[2].count1, '一周内': weekNum[2].count1, '一月内': monthNum[2].count1, '总计': allNum[2].count1 },
        { sysState: '订单管理','一天内': todayNum[3].count1, '一周内': weekNum[3].count1, '一月内': monthNum[3].count1, '总计': allNum[3].count1 },
        { sysState: '合同管理','一天内': todayNum[4].count1, '一周内': weekNum[4].count1, '一月内': monthNum[4].count1, '总计': allNum[4].count1 },
        { sysState: '售后管理','一天内': todayNum[5].count1, '一周内': weekNum[5].count1, '一月内': monthNum[5].count1, '总计': allNum[5].count1 },
        { sysState: '工单管理','一天内':todayNum[6].count1, '一周内': weekNum[6].count1, '一月内': monthNum[6].count1, '总计': allNum[6].count1 }
      ]
    },
    xAxis: { 
      type: 'category',
      axisPointer: {
        type: 'shadow'
      }
    },
    yAxis: {
        type: 'value',
        name: '(个)',
        min: 0,
        max: maxNum+Math.floor(maxNum/3),
        interval: Math.floor(maxNum/5),
        axisLabel: {
        formatter: '{value}'
      },
    },
    // Declare several bar series, each will be mapped
    // to a column of dataset.source by default.
    series: [{ type: 'bar',barWidth:30 }, 
             { type: 'bar',barWidth:30 }, 
             { type: 'bar',barWidth:30 },
             { type: 'bar',barWidth:30 }]
  };


  return (
    <div id='sysState'>
      <div className='cusStateTitle'>系统概况</div>
      <ReactECharts option={option} style={{width:"auto",height:"600px"}}/>
    </div>
  )
}


export default Index